<?php
//======================================================
// Copyright (C) 2004 John W. Holmes, All Rights Reserved
//
// This file is part of the Unit Command Climate
// Assessment and Survey System (UCCASS)
//
// UCCASS is free software; you can redistribute it and/or
// modify it under the terms of the Affero General Public License as
// published by Affero, Inc.; either version 1 of the License, or
// (at your option) any later version.
//
// http://www.affero.org/oagpl.html
//
// UCCASS is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// Affero General Public License for more details.
//======================================================

class UCCASS_Category extends UCCASS_Main
{
	function UCCASS_Category()
    { $this->load_configuration(); }

	/******************
    * NEW CATEGORY *
    ******************/
  
	function add_category()
    { 
        //Ensure user is logged in with valid privileges
        //for the requested survey or is an administrator
        if(!$this->_CheckLogin("category.php"))
        { return $this->showLogin('category.php',array('sid'=>$sid)); }	
		
		$error = '';
		$template = 'add_category.tpl';

        //The following values are also set
        //upon a successful submission to "reset"
        //the form...
        $category = '';
        $active = 0;

        if(isset($_REQUEST['submit']) || isset($_REQUEST['add_category']))
        {
            if(isset($_REQUEST['add_category']))
            { $ss_type = SAFE_STRING_TEXT; }
            else
            { $ss_type = SAFE_STRING_DB; }

            if(strlen($_REQUEST['category']) > 0)
            { 	
				$category = $this->SfStr->getSafeString($_REQUEST['category'],$ss_type); 
				
				//echo var_dump($category);
				$query = "SELECT 1 FROM {$this->CONF['db_tbl_prefix']}category WHERE category = '$category'";
				//echo $query;
                $rs = $this->Query($query,'Unable to see if matches another');
				if($rs->FetchRow($rs))
                { 
					$this->setMessageRedirect('add_category.php');
					$this->setMessage('Error','A category already exists with that name.',MSGTYPE_NOTICE);
				}
			}
        	if($_REQUEST['active'] == 1)
	        {$active = 1;}
		
			if(strlen($_REQUEST['category']) == 0)
            { 	$error = 'Please enter a name. '; 
				$this->setMessageRedirect('add_category.php');
				$this->setMessage('Error','Please add a category',MSGTYPE_NOTICE);
			}
			
			if(isset($_REQUEST['add_category']) && (!isset($error) || strlen($error) == 0))
            {
			    $privacidad = $_REQUEST["select"];
			    $cid = $this->db->GenID($this->CONF['db_tbl_prefix'].'category_sequence');
                $query = "INSERT INTO {$this->CONF['db_tbl_prefix']}category (cid, category, active, exit) VALUES
                          ($cid, '{$category}', $active, '{$privacidad}')";
			    
				$rs = $this->db->Execute($query);
                if($rs === FALSE)
                { $this->error("Error adding new category: " . $this->db->ErrorMsg());}
				else
				{ 
				$this->setMessageRedirect('category.php');
				$this->setMessage('&nbsp;','Categoria agregada correctamente',MSGTYPE_NOTICE);

				}							
            }
			
		}		
		else
		{
			/*$this->smarty->assign_by_ref('data',$data);
		    $retval = $this->smarty->Fetch($this->template.'/'.$template);
			return $retval;*/
			$this->setMessageRedirect('category.php');
		    echo "<META HTTP-EQUIV='Refresh' content='0; url=$this->_messageredirect'>";
			exit();
		}
    }

	/*******************
    * EDIT CATEGORY *
    *******************/
    function edit_category($edit)
    {
        
        //Ensure user is logged in with valid privileges
        //for the requested survey or is an administrator
        if(!$this->_CheckLogin("category.php"))
        { return $this->showLogin('category.php',array('sid'=>$sid)); }
		
		$cid = (int)$edit;

        //if(empty($cid))
        //{ return $this->edit_category_choose($cid); }

        $error = '';
        $show = array();
        $show['warning'] = FALSE;
        $load_category = TRUE;

        if(isset($_REQUEST['btn_edit']))
        {
		   
		    $error = '';
            $load_category = FALSE;

            if(strlen($_REQUEST['category']) > 0)
            { $input['category'] = $this->SfStr->getSafeString($_REQUEST['category'],$ss_type); }
            else
            { $error .= "Please enter a category. "; }
			
			$input['active'] = 0;
            if($_REQUEST['active'] == 1)
	        {$input['active'] = 1;}

	        
	        $input['salida'] = $_REQUEST['salida'];
	        
            if(empty($error))
            {

                $query = "UPDATE {$this->CONF['db_tbl_prefix']}category SET
                          category='{$input['category']}',active={$input['active']},
                          exit='{$input['salida']}' WHERE cid = $cid";

                
                $rs = $this->db->Execute($query);
                if($rs === FALSE)
                { $this->error("Error updating category: " . $this->db->ErrorMsg()); }
                else
                {
                    $load_category = TRUE;
                    $show['success']=TRUE;
                }
            }

            $this->smarty->assign_by_ref('category',$input);
			$this->setMessageRedirect('category.php');
			$this->setMessage('&nbsp;','Categoria editada correctamente',MSGTYPE_NOTICE);
        }

        if($load_category)
        {   /*
            $query = "SELECT cid, category, active, exit FROM {$this->CONF['db_tbl_prefix']}category WHERE cid = $cid";
            $rs = $this->db->Execute($query);
            if($rs === FALSE)
            { $this->error("Error selecting category information: " . $this->db->ErrorMsg()); return;}
            
			if($r = $rs->FetchRow($rs))
            {
                $category = array();
				
                //$category = $r;
				$category['cid'] = $this->SfStr->getSafeString($r['cid'],SAFE_STRING_TEXT);
                $category['category'] = $this->SfStr->getSafeString($r['category'],SAFE_STRING_TEXT);

				if($r['active'] == 1)
	            { $category['active_selected'] = ' checked'; }
    	        else
        	    { $category['inactive_selected'] = ' checked'; }
				
        	    if($r['exit'] == "pub")
	            { $category['pub_selected'] = ' selected'; }
    	        elseif($r['exit'] == "pri")
        	    { $category['pri_selected'] = ' selected'; }

				$this->smarty->assign_by_ref('category',$category);

            }
            else
            { $error = "Invalid category"; }
			*/
			
			$this->setMessageRedirect('category.php');
		    echo "<META HTTP-EQUIV='Refresh' content='0; url=$this->_messageredirect'>";
			exit();
        }

        if(!empty($error))
        {
            $input['category'] = $this->SfStr->getSafeString($_REQUEST['category'],SAFE_STRING_TEXT);
		
			$input['active'] = 0;
			if($_REQUEST['active'] == 1)
	        {$input['active'] = 1;}    
            $show['error'] = $error;
        }

        $data['cid'] = $input['cid'];
        $this->smarty->assign_by_ref('data',$data);
        $this->smarty->assign_by_ref('show',$show);

        $data['content'] = $this->smarty->Fetch($this->template.'/edit_category.tpl');

        $retval = $this->smarty->Fetch($this->template.'/edit_category.tpl');

        return $retval;
    }		
		
	/*****************************
    * CHOOSE CATEGORY TYPE TO EDIT *
    *****************************/
    function edit_category_choose($cid)
    {

        //Ensure user is logged in with valid privileges
        //for the requested survey or is an administrator
        if(!$this->_CheckLogin("category.php"))
        { return $this->showLogin('category.php',array('sid'=>$sid)); }
		
        $this->ChekNivel();
        
		$category = array();
        //$category['cid'] = $cid;
        
		$query = "SELECT cid, category, active, exit FROM {$this->CONF['db_tbl_prefix']}category ORDER BY category ASC";
		$rs = $this->db->Execute($query);
        if($rs === FALSE)
        { $this->error("Error selecting category: " . $this->db->ErrorMsg()); }
       
		while($r = $rs->FetchRow())
        {
			$category['cid'][] = $r['cid'];
            $category['category'][] = $this->SfStr->getSafeString($r['category'],SAFE_STRING_TEXT);
			$category['active'][] = $r['active'];
            $category['exit'][] = $this->SfStr->getSafeString($r['exit'],SAFE_STRING_TEXT);
        }
        
		$data['cid'] = $category['cid'];
        $this->smarty->assign('category',$category);
        $this->smarty->assign_by_ref('data',$data);

        $data['content'] = $this->smarty->Fetch($this->template.'/category.tpl');

        $this->smarty->assign_by_ref('show',$show);

        $retval = $this->smarty->Fetch($this->template.'/category.tpl');

        return $retval;
    }
}

?>